//
//  JPopoverPresentationC.swift
//  JMicroBlog
//
//  Created by Jason on 21/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

class JPopoverPresentationC: UIPresentationController {
    
    var presentFrame = CGRectZero
    
    // 遮罩视图
    lazy var dummyView : UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        // 添加手势识别 可以隔离所有底层的视图
        let tap = UITapGestureRecognizer(target: self, action:"clickDummyView")
        v.addGestureRecognizer(tap)
        return v
    }()
    
    //
    func clickDummyView() {
       // 关闭dummyView
       presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    /**
    UIPresentationControllerd的构造器
    
    :param: presentedViewController  要被modal出来的控制器
    :param: presentingViewController 文档说是底层控制器,但实测为nil
    
    :returns: 返回一个UIPresentationController对象
    
    // 重要属性 containView
    // The view in which the animated transition should take place.
    // containView用于给动画转场提供场所 就是一个容器,modal后的视图在个视图中显示
    // presented() 返回被展现的视图 
    // containerViewWillLayoutSubviews() 将要布局子视图 
         可在此自定义presented视图的大小和位置
    
    // containerViewDidLayoutSubviews() 子视图布局完成
    */
    override init(presentedViewController: UIViewController!, presentingViewController: UIViewController!) {
        JLog.i(presentedViewController,false)
        JLog.i(presentingViewController,false)
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 设置dummyView的位置
        dummyView.frame = containerView.bounds
        
        // 将dummyView设置到最底层
        containerView.insertSubview(dummyView, atIndex: 0)
        
        // 布局子视图
        presentedView().frame = presentFrame
    }
}
